<extend name="Public/base" />
<block name="content">
<div id="content">
	<div id="content-header">
		<div id="breadcrumb">
			<a class="tip-bottom" href="{:U('Index/index')}" data-original-title="首页"><i class="icon-home"></i> 首页</a>
			<a class="tip-bottom" href="{:U('Order/sale')}" data-original-title="">纸质订单管理</a> 
		</div>
	</div>
	<div class="container-fluid">
		<br/>
		<button class="btn btn-info" id="sale_button">销售纸质订单</button>
		<div class="row-fluid">
			<div class="span12">
				<div class="widget-box">
					<div class="widget-content">
						<center>请点击销售纸质订单~</center>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<!-- 整体表单 [模板]-->
<script type="text/template" id="tpl_form">
    <form id="j-popup-form">
        <label>活动列表</label>
        <div class="padding10">
            <select id="j-activity-list" class="form-control">
                <option>请选择活动</option>
                <volist name="activity_list" id="vo">
					<option value='{$vo.activity_id}'>{$vo.activity_name|strip_tags}</option>
				</volist>
            </select>
        </div>
        <label>参展店铺</label>
        <div class="padding10">
            <select id="j-shop-list" class="form-control" disabled>
                <option>请选择参展店铺</option>
            </select>
        </div>
        <label>纸质订单编号范围</label>
        <input onkeyup="value=value.replace(/[a-zA-z\.]+$/,'')" id="j-start-order" placeholder="最小订单号" class="order_id"/> <b>~</b> <input onkeyup="value=value.replace(/[a-zA-z\.]+$/,'')" id="j-end-order" placeholder="最大订单号" class="order_id"/>
</script>
<!-- /整体表单 [模板]-->

<!-- 店铺列表 [模板]-->
<script type="text/template" id="tpl_select_shop">
    <select id="j-shop-list" class="form-control">
	<option value="0">请选择店铺</option>
    <% for( k in data ){%>
        <% var item = data[k] %>
        <option value="<%=item.shop_id%>"><%=item.shop_name%></option>
    <%}%> 
    </select>
</script>
<!-- /店铺列表 [模板]-->
<script src="__STATIC__/aui-artDialog-9fd667e/dist/dialog.js"></script>
<link rel="stylesheet" href="__STATIC__/aui-artDialog-9fd667e/css/ui-dialog.css">
<script src="__JS__/underscore.js"></script>
<script src="__JS__/jjhh.js"></script>
<script type="text/javascript">
$(function (){
 	$('#sale_button').click(function(){
		dialog_bind();
	//	JJHH.Dialog.bindShop2Activity("{:U('Shop/getShopActivityList')}","{:U('Order/build')}");
	});
	/* set_value("activity_id",'{:I('activity_id')}') */
	/**
	 * @todo 根据活动获取店铺List
	 * @author PHP@妖孽 <dino_ma@163.com>
	 */
	$('#activity').change(function(){
		var _html;
		_html="<option value=0>选择店铺</option>";
		var activity_id = $(this).val();
		if(activity_id!=0){
			$.post("{:U('Shop/getShopActivityList')}", {activity_id:activity_id,data_type:1}, function(data){
				if(data.status==1){
					for(var k in data.data){
						_html += '<option value="'+data.data[k].shop_id+'">'+data.data[k].shop_name+'</option>';
					}
					$('#shop').html(_html);
				} else {
					$('#shop').html(_html);
					$('.alert').show(10);
				}
			}, "json");
			return false;
		}else{
			$('#shop').html(_html);
		}
	});
	 
	 
	 /*
	 *@todo 销售纸质订单
	 *@param ( string ) 商家列表接口
	 *@param ( string ) 绑定商家和活动接口
	 */
	 var dialog_bind = function() {
	     // 表单数据
	     var form_data      = {};
	     var form_tpl       = $( "#tpl_form" ).html();
	     var template       = _.template( form_tpl );
	     var form_html      = template(); 
	     // 对话框初始化
	     var d = dialog( {
	         "width": 360,
	         "content": form_html,
	         "okValue": "绑 定",
	         "title": "销售纸质订单",
	         "ok": function () {
	             form_data.activity_id      = $( "#j-activity-list" ).val();
	             form_data.shop_id          = $( "#j-shop-list" ).val();
	             form_data.order_start_num  = parseInt( $( "#j-start-order" ).val() );
	             form_data.order_end_num    = parseInt( $( "#j-end-order" ).val() );
	             // 绑定订单
	             _bind(form_data);
	             return false;
	         },
	         "cancelValue": '取消',
	         "cancel": function () {}
	     } ).showModal();
	     // 美化<select>
	     var $form = $("#j-popup-form");
	     $form.find("select").select2();

	     // XHR获取商家列表
	     $( "#j-activity-list" ).change( function(){
	         var activity_id = $( this ).val();
	         $( "#j-shop-list" ).select2("destroy").hide();
	         // 执行加载动画
	         var d_loading = dialog().content().showModal();
	         JJHH.Http.getshopList( "{:U('shop/getShopActivityList')}", activity_id, function( json ){
	             d_loading.close().remove();
	             if( 1 == json.status ){
	            	// 打开禁用
	                 $("#j-shop-list").removeProp("disabled");
	                 // 有数据
	                 var tpl            = $( "#tpl_select_shop" ).html();
	                 var template       = _.template( tpl );
	                 var html_shop_list = template( json ); 
	                 $( "#j-shop-list" ).html( html_shop_list );
	                 $( "#j-shop-list" ).select2();
	             }else{
	                 // 无数据
	                 $( "#j-shop-list" ).html( "<option value=\"0\">" + json.info + "</option>" );
	                 $( "#j-shop-list" ).select2();
	             }
	         } );
	     } );

	     // 最小订单按回车
	     $form.find("#j-start-order").keydown(function(event) {
	         if( 13 == event.keyCode ){
	             $form.find("#j-end-order").focus()
	         }
	     });

	     // 最大订单按回车
	     $form.find("#j-end-order").keydown(function(event) {
	         if( 13 == event.keyCode ){
	        	_bind(form_data);
	        	$(".ui-dialog-autofocus").blur();
	         } 
	     });    
	     function _bind(form_data){
	         var d_confirm = dialog({
	             "width": 240,
	             "title": "提示",
	             "content": "您确定要继续操作吗?",
	             "ok": function(){
	                 _bind_callback(form_data);
	             }, 
	             "okValue": "确定",
	             "cancelValue": "取消",
	             "cancel": function(){}
	         }).showModal();
	     }
	     // 绑定纸质和线上订单
	     function _bind_callback(form_data){
	    	 form_data.activity_id     = $( "#j-activity-list" ).val();
	         form_data.shop_id         = $( "#j-shop-list" ).val();
	         form_data.order_start_num = parseInt( $( "#j-start-order" ).val() );
	         form_data.order_end_num   = parseInt( $( "#j-end-order" ).val() );        
	         // console.log(form_data);
	         if( 1 > form_data.activity_id || isNaN(form_data.activity_id)){
	             dialog().content( "请选择\"活动\"" ).button( [{"value":"返回修改"}] ).showModal();
	             return false;
	         }else if( 1 > form_data.shop_id ||  isNaN(form_data.activity_id) ){
	             dialog().content( "\"参展店铺\"不能为空" ).button( [{"value":"返回修改"}] ).showModal();
	             return false;
	         }else if( 1 > form_data.order_start_num || isNaN(form_data.order_start_num) ){
	             dialog().content( "请检查\"最小订单号\"" ).button( [{"value":"返回修改"}] ).showModal();
	             return false;
	         }else if( 1 > form_data.order_end_num || isNaN(form_data.order_end_num) ){
	             dialog().content( "请检查\"最大订单号\"" ).button( [{"value":"返回修改"}] ).showModal();
	             return false;
	         }else if( form_data.order_start_num > form_data.order_end_num ){
	             dialog().content( "\"最大订单号\"不能小于\"最小定单号\"" ).button( [{"value":"返回修改"}] ).showModal();
	             return false;
	         }else{
	             var loading_dialog = dialog().title( "正在为您绑定数据..." ).showModal();
	             JJHH.Http.bindShop2Activity( "{:U('Order/build')}", form_data, function( json ){
	                 loading_dialog.remove().close();
	                 if( 1 == json.status ){
	                     var d_success = dialog().content( "提交成功,3秒钟自动关闭!" ).button( [{"value":"关闭"}] ).showModal();
	                     setTimeout( function(){
	                         d.remove().close();
	                         d_success.remove().close();
	                     }, 2000 ); 
	                     return false;
	                 }else{
	                     dialog().content( json.info ).button( [{"value":"返回"}] ).showModal();
	                     return false;
	                 }
	             } );
	         }
	         return false;
	     }
	 } // END
	 dialog_bind();
});
//导航高亮
highlight_subnav("{:U('Order/build')}");
</script>
</block>